package me.zhyx.music.service;


import lombok.extern.slf4j.Slf4j;
import me.zhyx.music.dao.MusicInterface;
import me.zhyx.music.model.Music;
import me.zhyx.music.model.Url;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Auther: yssq
 * @Date: 2019/3/14 15:07
 * @Description:
 */
@Service
@Slf4j
public class MusicService {
    @Autowired
    MusicInterface musicInterface;

    public int saveMusic(Music music) {
        Integer result = null;
        try {
            int insert = musicInterface.insert(music);
            log.info("insert success result is {},The id is {}", insert, music.getId());
            return insert;
        } catch (Exception e) {
            musicInterface.updateMusicDetails(music);
        }
        return result;
    }

    public void saveException(String url) {
        musicInterface.insertException(url);
    }

    public int updateMusic(String url, int isDelete) {
        return musicInterface.updateUrlStatus(url, isDelete);
    }

    public List<Url> selectExceptionUrl() {
        ArrayList<Url> result = new ArrayList<>();
        List<String> urls = musicInterface.selectAllUrl();
        for (String item : urls) {
            Url urlObj = new Url();
            urlObj.setLink(item);
            result.add(urlObj);
        }
        return result;
    }

    public int updateMusicDetails(Music music){
        return musicInterface.updateMusicDetails(music);
    }
}
